﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _028接口_依赖反转_单元测试
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //int[] num1 = { 1, 2, 3, 4, 5 };
            //ArrayList arrayList = new ArrayList { 1, 2, 3, 4, 5 };
            //Console.WriteLine(Sum(num1));
            //Console.WriteLine(Avg(num1));
            //Console.WriteLine(Sum(arrayList));
            //Console.WriteLine(Avg(arrayList));

            
            Engine engine = new Engine();
            Car car = new Car(engine);
            car.Run(3);
            Console.WriteLine(car.Speed);
        }


        //不使用数组 会使得方法很繁琐
        static int Sum(int[] nums)
        {
            int sum = 0;
            foreach (var item in nums)
            {
                sum += item;
            }
            return sum;
        }

        static double Avg(int[] nums)
        {
            int count = 0; double sum = 0;
            foreach (var item in nums)
            {
                sum += item;
                count++;
            }
            return sum / count;
        }
        static int Sum(ArrayList arrayList)
        {
            int sum = 0;
            foreach (var item in arrayList)
            {
                sum += (int)item;
            }
            return sum;
        }

        static double Avg(ArrayList arrayList)
        {
            int count = 0; double sum = 0;
            foreach (var item in arrayList)
            {
                sum += (int)item;
                count++;
            }
            return sum / count;
        }

    }

    class Engine
    {
        public int RPM { get; set; }
        public void Work(int gas)
        {
            this.RPM = gas * 1000;
        }
    }

    class Car
    {
        private Engine _engine;
        public Car(Engine engine)
        {
            this._engine = engine;
        }

        public int Speed { get; set; }

        public void Run(int gas)
        {
            _engine.Work(gas);
            this.Speed = _engine.RPM / 100;
        }
    }
}
